layui.use(['layer', 'element', 'form'], function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer;

    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块

    //表单
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
    // 排序方式事件
    $(".userex-ul").find("li").click(function () {
        $(".userex-ul").find("li").removeClass("active");
        $(this).addClass("active");
    });
    //批量管理
    $(".blog-index-button").find(".x").click(function () {
        $(this).hide()
        $(".blog-index-button").find(".y").show();
        $(".blog-index-managenment").show("300");
        $(".blog-index-list .select_this").show("300");
        $(".blog-index-list").find("#ledit,#time").hide("300");
    });
    $(".blog-index-button").find(".y").click(function () {
        $(this).hide()
        $(".blog-index-button").find(".x").show();
        $(".blog-index-managenment").hide("300");
        $(".blog-index-list .select_this").hide("300");
        $(".blog-index-list").find("#ledit,#time").show("300");
    });
    allselct();

    function allselct() {
        var checked = $("#select_all")[0];
        var checkson = $(".select_this");
        $(".all_select").find("span").click(function () {
            if ($("#select_all")[0].checked == false) {
                $("#select_all")[0].checked = true;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = true;
                }
            } else {
                $("#select_all")[0].checked = false;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = false;
                }
            }
        });
        $("#select_all").click(function () {
            if ($("#select_all")[0].checked == false) {
                $("#select_all")[0].checked = false;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = false;
                }
            } else {
                $("#select_all")[0].checked = true;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = true;
                }
            }
        });
    }

    //修改分类
    $(".blog-index-managenment").find(".onmanagement").click(function () {
        if ($(".blog-index-managenment").find(".management-class").is(":hidden")) {
            $(".blog-index-managenment").find(".management-class").show();
        } else {
            $(".blog-index-managenment").find(".management-class").hide();
        }
    });
    // 时间
    // var mydate = new Date();
    // var t = mydate.toLocaleString();
    /*alert(t);*/
    // $("#time").text(t);

    // 单个博客列表菜单
    setmenu();

    function setmenu() {
        $(".blog-list-item").find("#ledit").on("click", function (e) {
            e.stopPropagation();
            if ($(this).siblings(".management-list").is(":hidden")) {
                $(this).siblings(".management-list").show();
            } else {
                $(this).siblings(".management-list").hide();
            }
            $(document).click(function () {
                if ($(".blog-list-item").find(".management-list").is(":visible")) {
                    $(".blog-list-item").find(".management-list").hide();
                }
            });
        });
    }

    //管理
    $("#cate").click(function () {
        if ($(".blog-index-class .class-list").is(":visible")) {
            $(".blog-index-class .class-list").hide();
            $(".blog-index-class .class-list2").show();
        } else {
            $(".blog-index-class .class-list").show();
            $(".blog-index-class .class-list2").hide();
        }
    });
    //修改或添加分类
    $(".blog-index-class .class-list2 li").find("span").click(function () {
        if ($(this).text() == "修改") {
            $.ajax({
                url: "/Blog/updatecate",
                data: {cid: $(this).attr("value"), name: $(this).prev().val()},
                success(data) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000)
                }
            });
        } else if ($(this).text() == "添加") {
            $.ajax({
                url: "/Blog/addcate",
                data: {name: $(this).prev().val()},
                success(data) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000)
                }
            });

        }
    });
    //分类
    $(".blog-index-class .class-list").find("li").click(function () {
        $(".blog-index-class .class-list").find("li").removeClass("activeT");
        $(this).addClass("activeT");
        var cid = $(this).attr("value");
        var url = '/Blog/blogbycate/' + cid;
        if (cid == "0") {
            window.location.reload();
        }
        $.ajax({
            url: url,
            success: function (data) {
                $("#blog-first-list").hide();
                $("#blog-first-list").empty();
                for (var o in data.blogs) {
                    $("#blog-first-list").append(
                        "<li><div class='blog-list-item'>" +
                        "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                        "<span id='ledit'>编辑" +
                        "<i class='layui-icon'></i></span>" +
                        "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                        "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                        "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                        "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                        "<li onclick='click4(this)' value=" + data.blogs[o]["bid"] + ">设为私密</li>" +
                        "</ul>" +
                        "<input type='checkbox' class='select_this'>" +
                        "</div>" +
                        "</li>"
                    );
                }
                $("#blog-first-list").show("slow");
                setmenu();
            }
        });
    });
    layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
            ,
            count: 10 //数据总数，从服务端得到
        });
    });

    //
    opened = function (obj) {
        if ($(obj).text() == "我的博客") {
            $.ajax({
                url: '/Blog/opened/1',
                success: function (data) {
                    $("#blog-first-list").hide();
                    $("#blog-first-list").empty();
                    for (var o in data.blogs) {
                        $("#blog-first-list").append(
                            "<li><div class='blog-list-item'>" +
                            "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                            "<span id='ledit'>编辑" +
                            "<i class='layui-icon'></i></span>" +
                            "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                            "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                            "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                            "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                            "<li onclick='click4(this)' value=" + data.blogs[o]["bid"] + ">设为私密</li>" +
                            "</ul>" +
                            "<input type='checkbox' class='select_this'>" +
                            "</div>" +
                            "</li>"
                        );
                    }
                    $("#blog-first-list").show("slow");
                    setmenu();
                }
            });
        } else {
            $.ajax({
                url: '/Blog/opened/0',
                success: function (data) {
                    $("#blog-first-list").hide();
                    $("#blog-first-list").empty();
                    for (var o in data.blogs) {
                        $("#blog-first-list").append(
                            "<li><div class='blog-list-item'>" +
                            "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                            "<span id='ledit'>编辑" +
                            "<i class='layui-icon'></i></span>" +
                            "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                            "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                            "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                            "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                            "<li onclick='click5(this)' value=" + data.blogs[o]["bid"] + ">设为公开</li>" +
                            "</ul>" +
                            "<input type='checkbox' class='select_this'>" +
                            "</div>" +
                            "</li>"
                        );
                    }
                    $("#blog-first-list").show("slow");
                    setmenu();
                }
            });
        }
    };
    //进入blog详情页面
    click0 = function (obj) {
        window.location.href = '/Blog/blogdetail/' + $(obj).attr("value");
    };
    //进入blog修改
    click1 = function (obj) {
        window.location.href = '/Blog/blogedit/' + $(obj).val();
    };
    //删除blog
    click2 = function (obj) {
        $.ajax({
            url: '/Blog/blogdel2/' + $(obj).val(),
            success: function (data) {
                layer.msg(data.msg);
                $("#blog-first-list").empty();
                for (var o in data.blogs) {
                    $("#blog-first-list").append(
                        "<li><div class='blog-list-item'>" +
                        "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                        "<span id='ledit'>编辑" +
                        "<i class='layui-icon'></i></span>" +
                        "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                        "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                        "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                        "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                        "<li onclick='click4(this)' value=" + data.blogs[o]["bid"] + ">设为私密</li>" +
                        "</ul>" +
                        "<input type='checkbox' class='select_this'>" +
                        "</div>" +
                        "</li>"
                    );
                }
                setmenu();
            }
        });
    };
    //设为私密
    click4 = function (obj) {
        $("#blog-first-list").empty();
        $.ajax({
            url: '/Blog/changeop/1/' + $(obj).val(),
            success: function (data) {
                layer.msg(data.msg);
                for (var o in data.blogs) {
                    $("#blog-first-list").append(
                        "<li><div class='blog-list-item'>" +
                        "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                        "<span id='ledit'>编辑" +
                        "<i class='layui-icon'></i></span>" +
                        "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                        "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                        "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                        "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                        "<li onclick='click4(this)' value=" + data.blogs[o]["bid"] + ">设为私密</li>" +
                        "</ul>" +
                        "<input type='checkbox' class='select_this'>" +
                        "</div>" +
                        "</li>"
                    );
                }
                setmenu();
            }
        });
    };
    //设为公开
    click5 = function (obj) {
        $("#blog-first-list").empty();
        $.ajax({
            url: '/Blog/changeop/0/' + $(obj).val(),
            success: function (data) {
                layer.msg(data.msg);
                for (var o in data.blogs) {
                    $("#blog-first-list").append(
                        "<li><div class='blog-list-item'>" +
                        "<span onclick='click0(this)' value=" + data.blogs[o]["bid"] + ">" + data.blogs[o]["title"] + "</span>" +
                        "<span id='ledit'>编辑" +
                        "<i class='layui-icon'></i></span>" +
                        "<span id='time' >" + data.blogs[o]["date"] + "</span>" +
                        "<ul class='management-list  layui-anim layui-anim-upbit'>" +
                        "<li onclick='click1(this)' value=" + data.blogs[o]["bid"] + ">编辑博客</li>" +
                        "<li onclick='click2(this)' value=" + data.blogs[o]["bid"] + ">删除博客</li>" +
                        "<li onclick='click5(this)' value=" + data.blogs[o]["bid"] + ">设为公开</li>" +
                        "</ul>" +
                        "<input type='checkbox' class='select_this'>" +
                        "</div>" +
                        "</li>"
                    );
                }
                setmenu();
            }
        });
    };

    //批量操作
    delall = function (obj) {
        var li = "";
        $("#blog-first-list>li").each(function () {
            if ($(this).find(".select_this")[0].checked == true) {
                console.log($(this).find("span").eq(0).attr("value") + "ha ")
                li += $(this).find("span").eq(0).attr("value") + ",";
            }
        });
        $.ajax({
            url: '/Blog/blogdelall/' + li,
            success(data) {
                layer.msg(data.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 500)
            }
        })
    };
    changeca = function (obj) {
        var ca = $(obj).parent().attr("value");
        var li = "";
        $("#blog-first-list>li").each(function () {
            if ($(this).find(".select_this")[0].checked == true) {
                console.log($(this).find("span").eq(0).attr("value") + "ha ");
                li += $(this).find("span").eq(0).attr("value") + ",";
            }
        });
        $.ajax({
            url: '/Blog/blogchangeall/' + li + "/" + ca,
            success(data) {
                layer.msg(data.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 500)
            }
        })
    };
});
